package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Activity;
import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.service.ActivityService;
import cn.bdqn.t354.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    private ActivityService activityService;
    @Resource
    private CustomerService customerService;

    /**
     * 跳转交往记录页面
     * @param custNo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String custNo, Model model){
        //查询客户联系人信息
        List<Activity> atvList=activityService.getActivityByCustNo(custNo);
        model.addAttribute("atvList",atvList);

        //查询客户信息
        Customer customer=customerService.getCustomer(custNo);
        model.addAttribute("customer",customer);

        return "activity/list";
    }

    /**
     * 跳转新建交往记录人页面
     * @param custNo
     * @param custName
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(String custNo,String custName,Model model){
        model.addAttribute("custNo",custNo);
        model.addAttribute("custName",custName);
        return "activity/add";
    }

    /**
     * 保存新建交往记录
     * @param activity
     * @param model
     * @return
     */
    @RequestMapping("/save")
    public String save(Activity activity,Model model){
        try {
            activityService.addActivity(activity);
            //跳转列表页面
            return "redirect:/activity/list?custNo="+activity.getAtvCustNo();
        } catch (Exception e) {
            e.printStackTrace();
            //跳转新增页面
            return "redirect:/activity/add";
        }
    }


    /**
     * 跳转到交往记录编辑页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id,Model model){
        Activity activity=activityService.getActivity(id);
        model.addAttribute("activity",activity);

        return "activity/edit";
    }

    /**
     * 保存交往记录编辑信息
     * @param activity
     * @param model
     * @return
     */
    @RequestMapping("/update")
    public String update(Activity activity,Model model){
        try {
            activityService.updateActivity(activity);
            //跳转列表页面
            return "redirect:/activity/list?custNo="+activity.getAtvCustNo();
        } catch (Exception e) {
            e.printStackTrace();
            //跳转编辑页面
            return "redirect:/activity/edit";
        }
    }

    /**
     * 删除交往记录
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Long id,Model model){
        Map<String, Object> map = new HashMap<>();
        try {
            activityService.deleteActivity(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
}
